package org.example.controller.impl;

import org.example.controller.UserController;
import org.example.domain.User;
import org.example.domain.response.ResponseResult;
import org.example.services.impl.UserServiceImpl;
import org.example.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.ArrayUtils;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserControllerImpl implements UserController {
    @Autowired
    private UserServiceImpl userService;

    @Override
    @GetMapping("/{userId}")
    @PreAuthorize("hasAuthority('user:selectOne')")
    public ResponseResult getUserById(@PathVariable int userId) {
        return userService.getUserById(userId);
    }

    @Override
    @PostMapping()
    @PreAuthorize("hasAuthority('user:save')")
    public ResponseResult saveUser(@RequestBody @Validated(User.save.class) User user) {
        return userService.saveUser(user);
    }

    @Override
    @PutMapping("/3/{userId}")
    @PreAuthorize("hasAuthority('user:resetPassword')")
    public ResponseResult resetPasswordById(@PathVariable @NotNull(message = "用户ID不能为空") int userId) {
        return userService.resetPasswordById(userId);
    }

    @Override
    @DeleteMapping("/{userId}")
    @PreAuthorize("hasAuthority('user:delete')")
    public ResponseResult deleteUserById(@PathVariable int userId) {
        return userService.deleteUserById(userId);
    }

    @Override
    @PutMapping()
    @PreAuthorize("hasAuthority('user:update')")
    public ResponseResult updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @Override
    @GetMapping("/3/{pageSize}/{currentPage}")
    @PreAuthorize("hasAuthority('user:selectAll')")
    public ResponseResult getUserByPage(@PathVariable int currentPage,@PathVariable int pageSize) {
        return userService.getUserByPage(currentPage,pageSize);
    }

    @Override
    @GetMapping("/1/{pageSize}/{currentPage}/{username}")
    @PreAuthorize("hasAuthority('user:selectAll')")
    public ResponseResult getUserByUsername(@PathVariable int currentPage,@PathVariable int pageSize, @PathVariable String username) {
        return userService.getUserByUsername(currentPage,pageSize,username);
    }

    @Override
    @PutMapping("/2/{userId}")
    @PreAuthorize("hasAuthority('user:update')")
    public ResponseResult updateUserPassword(@PathVariable int userId,
                                             @RequestParam("password") @NotNull String encryptPassword,
                                             @RequestParam("newPassword") @NotNull String encryptNewPassword) {
        return userService.updateUserPassword(userId,encryptPassword,encryptNewPassword);
    }

    @Override
    @PostMapping("/icon/{userId}")
    @PreAuthorize("hasAuthority('user:update')")
    public ResponseResult uploadIcon(@RequestParam("file") MultipartFile file,@PathVariable int userId){
        return userService.uploadIcon(file ,userId);
    }

//    @Override
    @PostMapping("/2")
    @PreAuthorize("hasAuthority('user:selectAll')")
    public ResponseResult<List<Integer>> getAvatarByUserId(@RequestBody List<Integer> userIds) {
        System.out.println(userIds + "================================================");
//        return ResponseResult.success("jjj",userIds);
        return userService.getAvatarUserId(userIds);
    }


}
